#include <iostream>
#include <sstream>
#include <windows.h>
#include <TCHAR.h>
#include <dinput.h>
#include "..\DirectInput-Hook\Dinput8.h"
#include "NamedPipeClient.h"

int _tmain(int argc, TCHAR* argv[])
{
	
	std::cout<< "Done."<< std::endl
		<< "Waiting for server...(Press 'Escape' to exit, 'Space' to attempt connection now)"<< std::endl;

	NamedPipeClient clientPipe(_T("\\\\.\\pipe\\DInput-Hook"));

	while((GetAsyncKeyState(VK_SPACE) & 0x8000) == 0 && !clientPipe.IsServerAvailable())
	{
		if(GetAsyncKeyState(VK_ESCAPE) & 0x8000)
		{
			return 0;
		}

		Sleep(10);
	}

	std::cout<< "Attempting to connect..."<< std::endl;
	if(clientPipe.Connect())
	{
		std::cout<< "Connection succeeded. Sending key-presses for the keys: Escape, 1, 2, and 3..."<< std::endl
			<< "Press 'Escape' to exit."<<std::endl;

		KeyInfo keys[2];
		keys[0].dwData=0x80;
		keys[0].dwOfs=DIK_Q;
		keys[1].dwData=0x0;
		keys[1].dwOfs=DIK_Q;

		while((GetAsyncKeyState(VK_ESCAPE) & 0x8000) == 0)
		{
			if(!clientPipe.Write(keys,2))
			{
				std::cout<< "Failed to write the message: "<<std::endl;

				if(!clientPipe.IsServerAvailable())
				{
					std::cout<< "Server no longer exists - disconnecting..."<< std::endl;
					break;
				}
			}
			::Sleep(2000);
		}
	}
	else
		std::cout<< "Could not connect to server."<< std::endl;

	std::cout<< "Press enter to unhook and exit.."<<std::endl;
	std::cin.ignore();

	return 0;
}